*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Table D01	 								   *
********************************************************************************

cap log close 
log using ${log}/TableD01.log, replace 
clear 

use "$data/machinelearning_partialr.dta", clear

cap drop educ // ensure that if ed*, only dummy variables are used and not categorical educ var 

* starting to build up the list of covariates
global covlist1 "ln_wage firm_effect age1 age2 age3 tenure1 tenure2 tenure3 female firmsize1 firmsize2 firmsize3 separation_rate sdwages growth ed1 ed2 ed3 ed4 rd* i.occupation id* ir* ae*"

* create matrix / table  
matrix J=J(15,2,.)

* doing the regression to safe coefficients for table 
lassoregress delta_ln_wage_mover ${covlist1} if gsoep==0

* calculations of partial r-squared values of ML covariates
di "calculation of partial r-squared values"
	
local j = 0	
foreach covariate1 in "ln_wage" "firm_effect" "age1 age2 age3" "tenure1 tenure2 tenure3" "female" "firmsize1 firmsize2 firmsize3" "separation_rate" "sdwages" "growth" "ed1 ed2 ed3 ed4" "i.region1" "i.occupation" "i.industry" "ir*" "ae*" {
	
	local j = `j' + 1
	di "`covariate1'"
	
	* coefficient value 
	if !inlist("`covariate1'", "age1 age2 age3", "tenure1 tenure2 tenure3", "firmsize1 firmsize2 firmsize3", "ed1 ed2 ed3 ed4", "i.region1", "i.occupation", "i.industry", "ir*", "ae*"){
		mat J[`j',1] = _b[`covariate1']
	}
	if inlist("`covariate1'", "i.region1", "i.occupation", "i.industry", "ir*", "ae*"){
		mat J[`j',1] = .
	}
	if "`covariate1'" == "age1 age2 age3"{
		mat J[`j',1] = _b[age1]
	}
	if "`covariate1'" == "tenure1 tenure2 tenure3"{
		mat J[`j',1] = _b[tenure1]
	}	
	if "`covariate1'" == "firmsize1 firmsize2 firmsize3"{
		mat J[`j',1] = _b[firmsize3]
	}
	if "`covariate1'" == "ed1 ed2 ed3 ed4"{
		mat J[`j',1] = _b[ed3]
	}
}

local j = 0
foreach covariate1 in "ln_wage" "firm_effect" "age1 age2 age3" "tenure1 tenure2 tenure3" "female" "firmsize1 firmsize2 firmsize3" "separation_rate" "sdwages" "growth" "ed1 ed2 ed3 ed4" "i.region1" "i.occupation" "i.industry" "ir*" "ae*" {	
	
	local j = `j' + 1
	di "`covariate1'"	
	
	* partial R^2 
	local controls ""
	
	foreach covariate2 in "ln_wage" "firm_effect" "age1 age2 age3" "tenure1 tenure2 tenure3" "female" "firmsize1 firmsize2 firmsize3" "separation_rate" "sdwages" "growth" "ed1 ed2 ed3 ed4" "i.region1" "i.occupation" "i.industry" "ir*" "ae*" {
		if "`covariate2'"!="`covariate1'" local controls "`controls' `covariate2'"
	}
	
	quietly reg delta_ln_wage_mover `covariate1'
	di "unconditional r-squared"
	di e(r2)
	
	quietly reg delta_ln_wage_mover `controls' // partialling out
	quietly predict r_delta_ln_wage_mover, resid
	
	local covlist "" 
	
	if !(substr("`covariate1'",1,2)=="i." | substr("`covariate1'",3,3)=="*") {
		foreach cov in `covariate1' {
			quietly reg `cov' `controls' // partialling out
			quietly predict r_`cov', resid
			local covlist "`covlist' r_`cov'"
		}
	}
	if "`covariate1'"=="i.region1" {
		foreach cov of varlist rd* {
			quietly reg `cov' `controls' // partialling out
			quietly predict r_`cov', resid
			local covlist "`covlist' r_`cov'"
		}
	}
	if "`covariate1'"=="i.occupation" {
		quietly egen occgroup = group(occupation)
		quietly su occgroup
		local occmax = r(max)
		forvalues oc = 1/`occmax' {
			gen od`oc' = occgroup==`oc'
		}
		foreach cov of varlist od* {
			quietly reg `cov' `controls' // partialling out
			quietly predict r_`cov', resid
			local covlist "`covlist' r_`cov'"
		}
	}
	if "`covariate1'"=="i.industry" {
		foreach cov of varlist id* {
			quietly reg `cov' `controls' // partialling out
			quietly predict r_`cov', resid
			local covlist "`covlist' r_`cov'"
		}
	}
	if "`covariate1'"=="ir*" {
		foreach cov of varlist ir* {
			quietly reg `cov' `controls' // partialling out
			quietly predict r_`cov', resid
			local covlist "`covlist' r_`cov'"
		}
	}
	if "`covariate1'"=="ae*" {
		foreach cov of varlist ae* {
			quietly reg `cov' `controls' // partialling out
			quietly predict r_`cov', resid
			local covlist "`covlist' r_`cov'"
		}
	}
			
	quietly reg r_delta_ln_wage_mover `covlist'
	di "partial r-squared"
	di e(r2)
	mat J[`j',2] = round(e(r2), 0.001) 
	
	quietly drop r_delta_ln_wage_mover `covlist'
}


matrix list J 

* create matrix / output table		
cap noisily{
	frmttable using "${log}/TableD01.tex", tex fragment statmat(J) replace ///
	ctitle(" \textbf{Variable} & \textbf{Description}", "\textbf{Coefficient}", "\textbf{Partial $R^2$}") ///
	sdec(3,3) /// 
	rtitles("Wage & Mover´s Log Wage at Initial Firm." ///
	\ "Firm Effect & AKM Fixed Effect of Initial Firm." ///
	\ "Age in Years & Cubic in Mover´s Age (Linear Coef. Reported)." ///
	\ "Tenure in Years & Cubic in Mover´s Number of Years Spent at Initial Firm (Linear Coef. Reported)." ///
	\ "Gender & Female Dummy." ///
	\ "Firm Size & Cubic in Number of Employees at Initial firm (Coef. on Cubic, the Only Included Dummy, Reported)." /// 
	\ "Turnover & Annual Separation rate at Initial Firm." ///
	\ "Wage Dispersion & SD of Wages at Initial Firm." ///
	\ "Employment Growth & Annual Growth Rate in Number of Employees at Initial Firm." /// 
	\ "Education & Dummies for: No Education, Vocational Education, University Education, Omitted = Missing Education. Coef on University Education Reported, the Other Two Are Very Close to Zero." ///
	\ "Region & 16 \textit{Bundesländer} (German States)."  ///
	\ "Occupation & 1-Digit Occuation Categories."  ///
	\ "Industry & NACE Level 1 Codes."  ///
	\ "Industry $\times$ Region & Industry Dummies Interacted with Region Dummies."  ///
	\ "Age $\times$ Education & Cubic in Age Interacted with Education Dummies."  ///	
	) 
}
			


log close
clear